#include "SendHeart.h"
#include "SendRequest.h"
#include <cjson/cJSON.h>
#include <stdlib.h>
#include "AdDao.h"
#include "AdParse.h"

// 解析指令集
void parseCmdList(cJSON* item)
{
    if(cJSON_IsArray(item))
    {
        int size = cJSON_GetArraySize(item);
        for(int i=0;i<size;i++)
        {
            cJSON *cmdJson = cJSON_GetArrayItem(item,i);
            if(cJSON_IsNumber(cmdJson))
            {
                int num = cmdJson->valueint;
                switch (num)
                {
                case 1:
                    // system("shutdown");
                    printf("关机命令\n");
                    break;
                case 2:
                    // system("reboot");
                    printf("重启命令\n");
                default:
                    break;
                }
            }
        }
    }
}

// 解析上下结构
Ad* parseUpDownAd(cJSON* item){
    Ad* result = (Ad*)calloc(1,sizeof(Ad));
    if (cJSON_IsObject(item))
    {
        //解析广告
        cJSON *idJson = cJSON_GetObjectItemCaseSensitive(item,"id");
        int id = idJson->valueint;
        cJSON *templateIdJson = cJSON_GetObjectItemCaseSensitive(item,"template_id");
        int templateId = templateIdJson->valueint;
        cJSON *customerIdJson = cJSON_GetObjectItemCaseSensitive(item,"customer_id");
        int customerId = customerIdJson->valueint;
        cJSON *startTimeJson = cJSON_GetObjectItemCaseSensitive(item,"start_time");
        char *startTime = startTimeJson->valuestring;
        cJSON *endTimeJson = cJSON_GetObjectItemCaseSensitive(item,"end_time");
        char *endTime = endTimeJson->valuestring;
        int state = cJSON_GetObjectItemCaseSensitive(item,"state")->valueint;

        result->id  = id;
        result->template_id  = templateId;
        result->customer_id  = customerId;
        result->start_time  = startTime;
        result->end_time  = endTime;
        result->state = state;

        //广告资源解析
        AdResource *upAr = (AdResource*)calloc(1,sizeof(AdResource));
        upAr->ad_id = id;
        upAr->index = 0;
        cJSON *typeIdJson = cJSON_GetObjectItemCaseSensitive(item,"top_media_type_id");
        int typeId = typeIdJson->valueint;
        upAr->type_id = typeId;
        cJSON *urlJson = cJSON_GetObjectItemCaseSensitive(item,"top_media_url");
        char *url = urlJson->valuestring;
        upAr->path = url;
        cJSON *signJson = cJSON_GetObjectItemCaseSensitive(item,"top_media_sign");
        char *sign = signJson->valuestring;
        upAr->sign = sign;
        result->resources[0] =upAr;

        AdResource *buttomAr = (AdResource*)calloc(1,sizeof(AdResource));
        buttomAr->ad_id = id;
        buttomAr->index = 0;
        cJSON *typeIdJson1 = cJSON_GetObjectItemCaseSensitive(item,"buttom_media_type_id");
        int typeId1 = typeIdJson1->valueint;
        buttomAr->type_id = typeId1;
        cJSON *urlJson1 = cJSON_GetObjectItemCaseSensitive(item,"buttom_media_url");
        char *url1 = urlJson1->valuestring;
        buttomAr->path = url1;
        cJSON *signJson1 = cJSON_GetObjectItemCaseSensitive(item,"buttom_media_sign");
        char *sign1 = signJson1->valuestring;
        buttomAr->sign = sign1;
        result->resources[1] =buttomAr;
    }
    return result;
}

// 解析广告集
void parseAdList(cJSON* list){
    if(!cJSON_IsArray(list)){
        printf("解析失败\n");
        return;
    }
    
    int size = cJSON_GetArraySize(list);
    Ad* ads[size];
    for (int i = 0; i < size; i++)
    {
        cJSON* item = cJSON_GetArrayItem(list,i);
        int templateId = cJSON_GetObjectItemCaseSensitive(item,"template_id")->valueint;
        switch (templateId)
        {
            case 1:
                /* code */
                break;
            case 2:
                /* code */
                break;
            case 3:
                /* code */
                break;
            case 4:
                /* code */
                break;
            case 5:
                /* code */
                break;
            case 6:
                Ad* ad = parseUpDownAd(item);
                ads[i] = ad;
                break;
            default:
                break;
        }
    }

    // 下载资源，生成广告列表，保存到数据库
    int result = downloadResAndGenLocalAdListToSqlite(ads, size);
    if(result == 0){
        printf("本次媒体资源没有最新内容，无需通知qt\n");
    }else{
        printf("媒体资源已更新，请通知qt\n");
    }
    
}

size_t heartWriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
    size_t realsize = size * nmemb;
    // 这里可以处理获取到的数据，比如存储到文件或者其他操作
    char * result = (char *)contents;
    // 将json字符串转成json对象
    cJSON* root = cJSON_Parse(result);
    if(root != NULL)
    {
        cJSON* successJson = cJSON_GetObjectItemCaseSensitive(root,"success");
        cJSON* codeJson = cJSON_GetObjectItemCaseSensitive(root,"code");
        cJSON* messageJson = cJSON_GetObjectItemCaseSensitive(root,"message");

        if(successJson->valueint == 1 && codeJson->valueint == 200)
        {
            // 获取data中的数据
            cJSON* dataJson = cJSON_GetObjectItemCaseSensitive(root,"data");
            if(dataJson != NULL)
            {
                cJSON* adListJson = cJSON_GetObjectItemCaseSensitive(dataJson,"ad_list");
                cJSON* cmdListJson = cJSON_GetObjectItemCaseSensitive(dataJson,"cmd_list");

                // 解析cmd_list
                if(cmdListJson != NULL)
                {
                    parseCmdList(cmdListJson);
                    printf("解析cmd指令集处理结束\n");
                }
                // 解析ad_list
                if(adListJson != NULL){
                    parseAdList(adListJson);
                    printf("解析ad广告集处理结束\n");
                }
            }
        }
        else{
            printf("广告机信息心跳请求失败!原因=%s\n",messageJson->valuestring);
        }
    }

    // printf("%s\n",result);
    return realsize;
}

// 心跳请求
void sendHeartRequest(char* gatewayIp,char* sn){
    char* urlFormatStr = "http://%s/hearts.json?sn=%s";
    char *url = (char*)calloc(1,100);
    //2 构造url
    sprintf(url,urlFormatStr,gatewayIp,sn);
    printf("hearts request url:%s\n",url);
    sendGetRequest(url,heartWriteCallback);
}